package com.example.battle.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.example.battle.R;
import com.example.battle.db.BattleDao;
import com.example.battle.entity.Feedback;
import com.example.battle.entity.User;
import com.example.battle.util.ActivityCollector;
import com.example.battle.util.ToastUtils;

import java.util.Date;

public class FeedbackActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    private EditText feedback_content;
    private Button submit;
    private Spinner type;
    private String[] type_name;
    private ArrayAdapter<Feedback> adapter;
    private User user;
    private BattleDao battleDao;
    private ToastUtils toastUtils = new ToastUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);

        feedback_content = findViewById(R.id.feedback_content);
        submit = findViewById(R.id.feedback_submit);
        type = findViewById(R.id.feedback_type_spinner);
        type_name = getResources().getStringArray(R.array.feedback_type);

        adapter = new ArrayAdapter(FeedbackActivity.this,android.R.layout.simple_spinner_item,type_name);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        type.setAdapter(adapter);
        type.setSelection(0);
        type.setOnItemSelectedListener(this);

        Intent intent = getIntent();
        user = (User) intent.getSerializableExtra("user");
        submit.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String fb_content;
                Feedback feedback = new Feedback();
                feedback.setUserId(user.getUserId());
                feedback.setType(type.getSelectedItem().toString());
                feedback.setFeedbackDate(new Date().toString());
                fb_content = feedback_content.getText().toString();
                if (fb_content.equals("")){
                    toastUtils.showShort(FeedbackActivity.this,"反馈内容不能为空");
                    return;
                }
                feedback.setContent(fb_content);
                battleDao = Login.getInstance().getAppDB().battleDao();
                battleDao.addFeedback(feedback);
                toastUtils.showShort(FeedbackActivity.this,"反馈成功，我们会尽快处理");
                finish();
            }
        });
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
}
